//-----------------------------------------------------------------------------
// <copyright file="UIHelper.cs" 
//            company="Rail Europe Limited">
//     Copyright (c) Rail Europe Limited. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------------
namespace RailEurope.Cockpit
{
    #region Includes
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.IO;
    using System.IO.Compression;
    using System.Reflection;
    using System.Text;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    #endregion

    /// <summary>
    ///  UIHelper class will contain UI related methods.
    /// </summary>
    public class UIHelper
    {
        public static string RenderChildUserControl(string path, object entity)
        {
            Control viewControl = (new UserControl()).LoadControl(path);

            MethodInfo mth = viewControl.GetType().GetMethod("SetProperty");
            mth.Invoke(viewControl, new[] { entity });

            StringBuilder sb = new StringBuilder();
            StringWriter output = new StringWriter(sb);
            HtmlTextWriter htmlTW = new HtmlTextWriter(output);
            viewControl.RenderControl(htmlTW);

            return output.ToString();
        }

        #region Render Master user control
        public static string RenderUserControl(string path, object entity)
        {
            string materControlPath = "EmailTemplate/RailEuropeEmailMaster.ascx";
            Page pageHolder = new Page();
            UserControl viewControl = (UserControl)pageHolder.LoadControl(materControlPath);

            if (entity != null)
            {
                Type viewControlType = viewControl.GetType();


                PropertyInfo childControlPath = viewControlType.GetProperty("ChildControlPath");
                childControlPath.SetValue(viewControl, path, null);

                PropertyInfo childProperty = viewControlType.GetProperty("ChildControlProperty");
                childProperty.SetValue(viewControl, entity, null);
            }

            pageHolder.Controls.Add(viewControl);
            StringWriter output = new StringWriter();
            HttpContext.Current.Server.Execute(pageHolder, output, true);

            return output.ToString();
        }

        #endregion
    }
}